layui.use(['table', 'admin', 'ax', 'func','laydate','formSelects', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 社区管理
     */
    var NanSellAnnounce = {
        tableId: "nanSellAnnounceTable"
    };

    var enumAjax = new $ax(Feng.ctxPath + "/enums/checkStatus");
    var enumRes = enumAjax.start();
    var projects = enumRes.data;
    var html = "";
    for(var key in projects){
        html += '<option value="'+key+'">'+projects[key]+'</option>'
    }
    $("#checkStatus").append(html);
    form.render('select');

    /**
     * 初始化表格的列
     */
    NanSellAnnounce.initColumn = function () {
        return [[
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 240},
            {field: 'checkStatusStr', sort: false, title: '审核状态'},
            {field: 'sort', sort: true, title: '排序'},
            {field: 'thumb', width: 200,sort: false, title: '图片', templet: function(d){
                    if (!d.thumb || d.thumb === '-' || d.thumb === '_') return '';
                    const urls = d.thumb.split(',').filter(url => url && url.trim() !== '' && url !== '-' && url !== '_');
                    if (urls.length === 0) return '';
                    return urls.map(url => `<img src="${Feng.cdn + url}" style="height:60px;margin-right:5px;">`).join('');
                }},
            {field: 'video', width: 80, sort: false, title: '视频', templet: function(d){
                    if (!d.video) return '';
                    const urls = d.video.split(',');
                    return urls.map((url, index) => `<a href="${Feng.cdn + url}" target="_blank" style="height:60px;margin-right:5px; text-decoration: underline; font-size: 12px;">查看</a>`).join('');
                }},
            {field: 'name', sort: false, title: '标题'},
            {field: 'content', sort: false, title: '内容'},
            {field: 'isDel', sort: false, title: '是否删除', templet: '<div>{{d.isDel? "是":"否"}}</div>'},
            {field: 'sellerName', sort: false, title: '用户昵称'},
            {field: 'userId', sort: false, title: '用户ID'},
            {field: 'commentTimes', sort: false, title: '评论数'},
            {field: 'likeTimes', sort: false, title: '点赞数'},
            {field: 'collectTimes', sort: false, title: '收藏数'},
            {field: 'showTimes', sort: false, title: '曝光量'},
            {field: 'clickTimes', sort: false, title: '点击量'},
            {field: 'isOfficial', sort: false, title: '是否官方', templet: '<div>{{d.isOfficial? "是":"否"}}</div>'},
            {field: 'backReason', sort: false, title: '不通过原因'},
            {field: 'createTime', sort: false, title: '创建时间'},
            {field: 'updateTime', sort: false, title: '更新时间'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    NanSellAnnounce.search = function () {
        var queryData = {};

        queryData['name'] = $('#name').val();
        queryData['checkStatus'] = $('#checkStatus').val();

        table.reload(NanSellAnnounce.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    NanSellAnnounce.openAddDlg = function () {
        func.open({
            title: '添加社区',
            content: Feng.ctxPath + '/nanSellAnnounce/add',
            tableId: NanSellAnnounce.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      NanSellAnnounce.openEditDlg = function (data) {
          func.open({
              title: '社区文章审核',
              content: Feng.ctxPath + '/nanSellAnnounce/edit?id=' + data.id,
              tableId: NanSellAnnounce.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    NanSellAnnounce.exportExcel = function () {
        var checkRows = table.checkStatus(NanSellAnnounce.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    NanSellAnnounce.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/nanSellAnnounce/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(NanSellAnnounce.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + NanSellAnnounce.tableId,
        url: Feng.ctxPath + '/nanSellAnnounce/list',
        page: true,
        limit: 20,
        height: "full-158",
        cellMinWidth: 100,
        cols: NanSellAnnounce.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        NanSellAnnounce.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    NanSellAnnounce.openAddDlg();

    });

    // 发布官方帖子按钮点击事件
    $('#btnAddOfficial').click(function () {
        func.open({
            title: '发布官方帖子',
            content: Feng.ctxPath + '/nanSellAnnounce/addOfficial',
            tableId: NanSellAnnounce.tableId
        });
    });

    // 导出excel
    $('#btnExp').click(function () {
        NanSellAnnounce.exportExcel();
    });

    table.on('sort('+NanSellAnnounce.tableId+')', function(obj){
            table.reload(NanSellAnnounce.tableId, {
                initSort: obj,
                where: {
                    field: obj.field.replace(/([A-Z])/g,"_$1").toLowerCase()
                    ,order: obj.type
                }
            });
        });

    // 工具条点击事件
    table.on('tool(' + NanSellAnnounce.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            NanSellAnnounce.openEditDlg(data);
        }else if (layEvent === 'sort') {
            func.open({
                title: '修改排序',
                content: Feng.ctxPath + '/nanSellAnnounce/editSort?id=' + data.id,
                tableId: NanSellAnnounce.tableId
            });
        } else if (layEvent === 'delete') {
            NanSellAnnounce.onDeleteItem(data);
        }else if(layEvent === 'look'){
            admin.open({
                id: 'lookForm',
                type: 2,
                title: "查看评论",
                shade: 0,
                area: ['1100px', '700px'],
                content: '/nanSellAnnounceComment?announceId=' + data.id
            });
        }else if(layEvent === 'advert'){
            admin.open({
                id: 'advertForm',
                type: 2,
                title: "帖子广告管理",
                shade: 0,
                area: ['1100px', '700px'],
                content: '/nanSellAdvert/list?sellAnnounceId=' + data.id
            });
        }
    });
});
